package com.leetcode;


import org.junit.Test;

import java.util.Arrays;
import java.util.Comparator;
import java.util.Map;
import java.util.TreeMap;

/**
 * Created by Z-hong on 2022/9/19.
 */

public class LeetCode1636 {
    public int[] frequencySort(int[] nums) {
        int max=1;
       /* TreeMap 会对内部进行升序排序，Comparator.reverseOrder()会将排序反转成为降序*/
        Map<Integer, Integer> maps = new TreeMap<Integer, Integer>(Comparator.reverseOrder());
        for (int i = 0; i < nums.length; i++) {
            if(maps.containsKey(nums[i])){
                maps.put(nums[i],maps.get(nums[i])+1);
                max=Math.max(max, maps.get(nums[i]));
            }else {
                maps.put(nums[i],1);
            }
        }
        int count=0;
        for (int i = 1; i <=max ; i++) {
            for(Map.Entry<Integer,Integer> entry: maps.entrySet()){
                if(entry.getValue()==i){
                    for (int j = 0; j < i; j++) {
                        nums[count++]=entry.getKey();
                    }
                }
            }
        }
        return nums;
    }

    @Test
    public void test(){
        int[] arr={1,1,2,2,2,3};
        arr=frequencySort(arr);
        System.out.println(Arrays.toString(arr));
    }
}
